from sqlalchemy.orm import Session
from app.db.models import User, Resume, PersonalInfo, Education, WorkExperience, Skill, Certificate, JobDescription
from app.schemas import (
    UserCreate, UserUpdate, 
    ResumeCreate, ResumeUpdate, 
    PersonalInfoCreate, PersonalInfoUpdate,
    EducationCreate, EducationUpdate,
    WorkExperienceCreate, WorkExperienceUpdate,
    SkillCreate, SkillUpdate,
    CertificateCreate, CertificateUpdate,
    JobDescriptionCreate
)
from passlib.context import CryptContext

# 密码加密上下文
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

# 用户相关CRUD操作
def get_user(db: Session, user_id: int):
    return db.query(User).filter(User.id == user_id).first()

def get_user_by_email(db: Session, email: str):
    return db.query(User).filter(User.email == email).first()

def get_user_by_username(db: Session, username: str):
    return db.query(User).filter(User.username == username).first()

def get_users(db: Session, skip: int = 0, limit: int = 100):
    return db.query(User).offset(skip).limit(limit).all()

def create_user(db: Session, user: UserCreate):
    hashed_password = pwd_context.hash(user.password)
    db_user = User(
        username=user.username,
        email=user.email,
        hashed_password=hashed_password
    )
    db.add(db_user)
    db.commit()
    db.refresh(db_user)
    return db_user

def update_user(db: Session, db_user: User, user: UserUpdate):
    if user.username: db_user.username = user.username
    if user.email: db_user.email = user.email
    if user.password: db_user.hashed_password = pwd_context.hash(user.password)
    db.commit()
    db.refresh(db_user)
    return db_user

def delete_user(db: Session, user_id: int):
    db_user = get_user(db, user_id)
    if db_user:
        db.delete(db_user)
        db.commit()
        return True
    return False

def authenticate_user(db: Session, username: str, password: str):
    user = get_user_by_username(db, username)
    if not user:
        return False
    if not pwd_context.verify(password, user.hashed_password):
        return False
    return user

# 简历相关CRUD操作
def get_resume(db: Session, resume_id: int):
    return db.query(Resume).filter(Resume.id == resume_id).first()

def get_resumes_by_user(db: Session, user_id: int, skip: int = 0, limit: int = 100):
    return db.query(Resume).filter(Resume.user_id == user_id).offset(skip).limit(limit).all()

def create_resume(db: Session, resume: ResumeCreate, user_id: int):
    db_resume = Resume(**resume.dict(), user_id=user_id)
    db.add(db_resume)
    db.commit()
    db.refresh(db_resume)
    return db_resume

def update_resume(db: Session, db_resume: Resume, resume: ResumeUpdate):
    if resume.title: db_resume.title = resume.title
    if resume.template_type: db_resume.template_type = resume.template_type
    if resume.is_active is not None: db_resume.is_active = resume.is_active
    db.commit()
    db.refresh(db_resume)
    return db_resume

def delete_resume(db: Session, resume_id: int):
    db_resume = get_resume(db, resume_id)
    if db_resume:
        db.delete(db_resume)
        db.commit()
        return True
    return False

# 个人信息相关CRUD操作
def get_personal_info(db: Session, resume_id: int):
    return db.query(PersonalInfo).filter(PersonalInfo.resume_id == resume_id).first()

def create_personal_info(db: Session, personal_info: PersonalInfoCreate, resume_id: int):
    db_personal_info = PersonalInfo(**personal_info.dict(), resume_id=resume_id)
    db.add(db_personal_info)
    db.commit()
    db.refresh(db_personal_info)
    return db_personal_info

def update_personal_info(db: Session, db_personal_info: PersonalInfo, personal_info: PersonalInfoUpdate):
    for key, value in personal_info.dict(exclude_unset=True).items():
        setattr(db_personal_info, key, value)
    db.commit()
    db.refresh(db_personal_info)
    return db_personal_info

def delete_personal_info(db: Session, personal_info_id: int):
    db_personal_info = db.query(PersonalInfo).filter(PersonalInfo.id == personal_info_id).first()
    if db_personal_info:
        db.delete(db_personal_info)
        db.commit()
        return True
    return False

# 教育经历相关CRUD操作
def get_education(db: Session, education_id: int):
    return db.query(Education).filter(Education.id == education_id).first()

def get_educations_by_resume(db: Session, resume_id: int):
    return db.query(Education).filter(Education.resume_id == resume_id).all()

def create_education(db: Session, education: EducationCreate, resume_id: int):
    db_education = Education(**education.dict(), resume_id=resume_id)
    db.add(db_education)
    db.commit()
    db.refresh(db_education)
    return db_education

def update_education(db: Session, db_education: Education, education: EducationUpdate):
    for key, value in education.dict(exclude_unset=True).items():
        setattr(db_education, key, value)
    db.commit()
    db.refresh(db_education)
    return db_education

def delete_education(db: Session, education_id: int):
    db_education = get_education(db, education_id)
    if db_education:
        db.delete(db_education)
        db.commit()
        return True
    return False

# 工作经历相关CRUD操作
def get_work_experience(db: Session, work_experience_id: int):
    return db.query(WorkExperience).filter(WorkExperience.id == work_experience_id).first()

def get_work_experiences_by_resume(db: Session, resume_id: int):
    return db.query(WorkExperience).filter(WorkExperience.resume_id == resume_id).all()

def create_work_experience(db: Session, work_experience: WorkExperienceCreate, resume_id: int):
    db_work_experience = WorkExperience(**work_experience.dict(), resume_id=resume_id)
    db.add(db_work_experience)
    db.commit()
    db.refresh(db_work_experience)
    return db_work_experience

def update_work_experience(db: Session, db_work_experience: WorkExperience, work_experience: WorkExperienceUpdate):
    for key, value in work_experience.dict(exclude_unset=True).items():
        setattr(db_work_experience, key, value)
    db.commit()
    db.refresh(db_work_experience)
    return db_work_experience

def delete_work_experience(db: Session, work_experience_id: int):
    db_work_experience = get_work_experience(db, work_experience_id)
    if db_work_experience:
        db.delete(db_work_experience)
        db.commit()
        return True
    return False

# 技能相关CRUD操作
def get_skill(db: Session, skill_id: int):
    return db.query(Skill).filter(Skill.id == skill_id).first()

def get_skills_by_resume(db: Session, resume_id: int):
    return db.query(Skill).filter(Skill.resume_id == resume_id).all()

def create_skill(db: Session, skill: SkillCreate, resume_id: int):
    db_skill = Skill(**skill.dict(), resume_id=resume_id)
    db.add(db_skill)
    db.commit()
    db.refresh(db_skill)
    return db_skill

def update_skill(db: Session, db_skill: Skill, skill: SkillUpdate):
    for key, value in skill.dict(exclude_unset=True).items():
        setattr(db_skill, key, value)
    db.commit()
    db.refresh(db_skill)
    return db_skill

def delete_skill(db: Session, skill_id: int):
    db_skill = get_skill(db, skill_id)
    if db_skill:
        db.delete(db_skill)
        db.commit()
        return True
    return False

# 证书相关CRUD操作
def get_certificate(db: Session, certificate_id: int):
    return db.query(Certificate).filter(Certificate.id == certificate_id).first()

def get_certificates_by_resume(db: Session, resume_id: int):
    return db.query(Certificate).filter(Certificate.resume_id == resume_id).all()

def create_certificate(db: Session, certificate: CertificateCreate, resume_id: int):
    db_certificate = Certificate(**certificate.dict(), resume_id=resume_id)
    db.add(db_certificate)
    db.commit()
    db.refresh(db_certificate)
    return db_certificate

def update_certificate(db: Session, db_certificate: Certificate, certificate: CertificateUpdate):
    for key, value in certificate.dict(exclude_unset=True).items():
        setattr(db_certificate, key, value)
    db.commit()
    db.refresh(db_certificate)
    return db_certificate

def delete_certificate(db: Session, certificate_id: int):
    db_certificate = get_certificate(db, certificate_id)
    if db_certificate:
        db.delete(db_certificate)
        db.commit()
        return True
    return False

# 职位描述相关CRUD操作
def get_job_description(db: Session, job_description_id: int):
    return db.query(JobDescription).filter(JobDescription.id == job_description_id).first()

def get_job_descriptions(db: Session, skip: int = 0, limit: int = 100):
    return db.query(JobDescription).offset(skip).limit(limit).all()

def create_job_description(db: Session, job_description: JobDescriptionCreate):
    db_job_description = JobDescription(**job_description.dict())
    db.add(db_job_description)
    db.commit()
    db.refresh(db_job_description)
    return db_job_description

def update_job_description(db: Session, db_job_description: JobDescription, key_skills: str = None):
    if key_skills: db_job_description.key_skills = key_skills
    db.commit()
    db.refresh(db_job_description)
    return db_job_description

def delete_job_description(db: Session, job_description_id: int):
    db_job_description = get_job_description(db, job_description_id)
    if db_job_description:
        db.delete(db_job_description)
        db.commit()
        return True
    return False